# _*_coding : UTF-8_*_
# 开发人员 : daizhe
# 开发时间 : 2021/1/30 17:31
from kubernetes import client, config
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
import os

# 登录认证检查
def auth_check(auth_type, str):
    if auth_check == "token":
        token = str
        configuration = client.Configuration()
        configuration.host = "https://172.17.64.138:6443"  # APISERVER地址
        # configuration.ssl_ca_cert=r".\ca.crt"  # CA证书
        configuration.verify_ssl = False   # 启用证书验证
        # kubectl describe secret/$(kubectl get secret -nkube-system |grep admin|awk '{print $1}') -nkube-system
        # token = "eyJhbGciOiJSUzI1NiIsImtpZCI6IlRDM2hwNlM1ak1vUDZ2NjFtNDB4cEIzT0NlRDZzVmxPSFZiYzlqOEdXQzAifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLTV4bGtiIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiI4NWZiMDMyZC05ZTI2LTRjMDgtOGY2Yi0yNDIwZGMxMTA5NDkiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZS1zeXN0ZW06YWRtaW4tdXNlciJ9.JqaGNnGflGQE7ENiXL9JFtwcBcS1PS3-JGmJX7bdStLm6fNENmtKXIa2L5RM8hzX3zwGZEkw3jKX1Ult6ug7e_EU5yXsP0BZ6lNhowfu6C0vYbEX4o-lJV2sX7DHFp9_-urmtjBeXHaf88J3vJ_Fbg8Yv_5Mfk3cCC0yiRZ7EAAUp7d8H2bjVPIvtGSkxFK4WrL0cj0lsrQZg7vgxDbclJJVCpWDDz8NLINjqURpqpfWGo0ABTwYQ_bV8tdXt9hTPAfVDXNTJYm4cHhP-fDQKjkUH-PGzmo56_e-aT24faIOQQEEefgQPw88ScUGY8UgXdfMXTv_tPJj-A588VSmMg"
        configuration.api_key = {"authorization": "Bearer " + token}  # 指定Token字符串
        client.Configuration.set_default(configuration)
        try:
            core_api = client.CoreApi()
            core_api.get_api_versions() # 查询api版本测试
            print('aaaaaaaaaaaaaaaaaaaaaa')
            return True
        except Exception as e:
            print(e)
            return False
    elif auth_type == "kubeconfig":
        random_str = str
        file_path = os.path.join('kubeconfig', random_str)
        config.load_kube_config(r"%s" % file_path)
        try:
            core_api = client.CoreApi()
            core_api.get_api_versions()  # 查询资源测试
            return True
        except Exception as e:
            print(e)
            return False

# 登录认证装饰器(通过session的方式判断，未登陆过不能访问/)
from django.shortcuts import render, redirect
def self_login_required(func):
    def inner(request, *args, **kwargs):
        is_login = request.session.get('is_login', False)
        if is_login:
            return func(request, *args, **kwargs)
        else:
            return redirect("/login")
    return inner


# 加载认证配置
def load_auth_config(auth_type, str):
    if auth_type == "token":
        token = str
        configuration = client.Configuration()
        configuration.host = "https://172.17.64.138:6443"  # APISERVER地址
        # configuration.ssl_ca_cert=r".\ca.crt"  # CA证书
        configuration.verify_ssl = False   # 启用证书验证
        # kubectl describe secret/$(kubectl get secret -nkube-system |grep admin|awk '{print $1}') -nkube-system
        # token = "eyJhbGciOiJSUzI1NiIsImtpZCI6IlRDM2hwNlM1ak1vUDZ2NjFtNDB4cEIzT0NlRDZzVmxPSFZiYzlqOEdXQzAifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLTV4bGtiIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiI4NWZiMDMyZC05ZTI2LTRjMDgtOGY2Yi0yNDIwZGMxMTA5NDkiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZS1zeXN0ZW06YWRtaW4tdXNlciJ9.JqaGNnGflGQE7ENiXL9JFtwcBcS1PS3-JGmJX7bdStLm6fNENmtKXIa2L5RM8hzX3zwGZEkw3jKX1Ult6ug7e_EU5yXsP0BZ6lNhowfu6C0vYbEX4o-lJV2sX7DHFp9_-urmtjBeXHaf88J3vJ_Fbg8Yv_5Mfk3cCC0yiRZ7EAAUp7d8H2bjVPIvtGSkxFK4WrL0cj0lsrQZg7vgxDbclJJVCpWDDz8NLINjqURpqpfWGo0ABTwYQ_bV8tdXt9hTPAfVDXNTJYm4cHhP-fDQKjkUH-PGzmo56_e-aT24faIOQQEEefgQPw88ScUGY8UgXdfMXTv_tPJj-A588VSmMg"
        configuration.api_key = {"authorization": "Bearer " + token}  # 指定Token字符串
    elif auth_type == "kubeconfig":
        random_str = str
        file_path = os.path.join('kubeconfig', random_str)
        config.load_kube_config(r"%s" % file_path)

# 时间格式化
from datetime import date,timedelta
def dt_format(timestamp):
    t = date.strftime((timestamp + timedelta(hours=8)), '%Y-%m-%d %H:%M:%S')
    return t
